//
//  HomeViewController.m
//  LianShangBao
//
//  Created by 主事丫环HU on 2020/8/28.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeTableHeaderView.h"
#import "HomeTableViewCell.h"
#import "HomeBuyNowController.h"// 立即购买弹框
#import "ProductListModel.h"
#import "IssueNowAlertController.h"// 立即发放弹框
#import "NoMoneyAlertController.h"// 余额不足弹框
#import "RechargeController.h"
#define NibWithClass(Class)  [UINib nibWithNibName:NSStringFromClass([Class class]) bundle:nil]
#define StringWithClass(Class) NSStringFromClass([Class class])
@interface HomeViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSString *typeID;
@property (nonatomic,strong) NSMutableArray *dataArray;
@end

@implementation HomeViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    // 写这个东西是为了 防止tableview的header滑动到屏幕外边，headerView里面会再次请求产品的数据
    NSString *isFirst = @"1";
    [USER_DEFAULT setObject:isFirst forKey:@"isFirst"];
    self.dataArray = [NSMutableArray arrayWithCapacity:0];
}
- (void)setupUI{
    [self.tableView registerNib:NibWithClass(HomeTableHeaderView) forHeaderFooterViewReuseIdentifier:StringWithClass(HomeTableHeaderView)];
    self.tableView.rowHeight = 100;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] init];
     if (@available(iOS 11.0,*)) {
         self.tableView.contentInsetAdjustmentBehavior=UIScrollViewContentInsetAdjustmentNever;
     }else{
         self.automaticallyAdjustsScrollViewInsets=NO;
     }
}
- (void)requestProductData{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"typeId"] = self.typeID;
    [NetWorkTool POST:getGoodsList param:param success:^(id dic) {
        self.dataArray = [ProductListModel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        NSIndexSet *set = [NSIndexSet indexSetWithIndex:1];
        [self.tableView reloadSections:set withRowAnimation:UITableViewRowAnimationFade];
    } other:^(id dic) {
        
    } fail:^(NSError *error) {
        
    } needUser:NO];
}
#pragma mark - tableview delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 445;
    } else {
        return 0.001;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        HomeTableHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:StringWithClass(HomeTableHeaderView)];
        [headerView Assignment];
        headerView.didSelectedTypeClick = ^(NSString * _Nonnull typeID) {
            self.typeID = typeID;
            [self requestProductData];
        };
        return headerView;
    } else {
        return [[UIView alloc] init];
    }
    
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footerView = [[UIView alloc] init];
    return footerView;
}
#pragma mark - tableview datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    } else {
        return self.dataArray.count;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ProductListModel *model = self.dataArray[indexPath.row];
    HomeTableViewCell *cell = [HomeTableViewCell cellWithTableView:tableView];
    cell.model = model;
    [cell.buyNowButton addTarget:self action:@selector(buyNowButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    cell.buyNowButton.tag = indexPath.row;
    return cell;
}
- (void)buyNowButtonClick:(UIButton *)sender{
    ProductListModel *model = self.dataArray[sender.tag];
    HomeBuyNowController * vc = [[HomeBuyNowController alloc] init];
    vc.model = model;
    __weak typeof(vc) weakVc = vc;
    vc.canBuyClick = ^(double totalMoney, NSString * _Nonnull balance,NSString * _Nonnull count) {
        [weakVc dismissViewControllerAnimated:YES completion:nil];
        IssueNowAlertController * vc2 = [[IssueNowAlertController alloc] init];
        vc2.balance = balance;
        vc2.total = totalMoney;
        vc2.count = count;
        vc2.productID = model.id;
        vc2.buySuccessfulClick = ^{
            model.num = [NSString stringWithFormat:@"%ld",model.num.integerValue + count.integerValue];
            [self.tableView reloadData];
        };
        [self presentViewController:vc2 animated:YES completion:nil];
    };
    vc.noMoneyClick = ^(double totalMoney, NSString * _Nonnull balance) {// 弹出余额不足
        [weakVc dismissViewControllerAnimated:YES completion:nil];
        NoMoneyAlertController * vc3 = [[NoMoneyAlertController alloc] init];
        vc3.balance = balance;
        vc3.total = totalMoney;
        vc3.pushRechargeClick = ^{
            RechargeController * vc = [[RechargeController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        };
        [self presentViewController:vc3 animated:YES completion:nil];
    };
    [self presentViewController:vc animated:YES completion:nil];
}

@end
